package com.tyrant.houtai;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.sunshine.retrofit.HttpUtil;
import com.sunshine.retrofit.interfaces.ErrorFunListener;
import com.sunshine.retrofit.interfaces.Success;
import com.tyrant.houtai.adapter.SMSAdapter;
import com.tyrant.houtai.bean.SMSBean;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SmsActivity extends AppCompatActivity {

    @Bind(R.id.et_phone)
    EditText et_phone;
    @Bind(R.id.btn_query)
    Button btn_query;
    @Bind(R.id.rv_list)
    RecyclerView rv_list;
    private ArrayList<SMSBean.ResultBean> contactBeanArrayList = new ArrayList<>();
    private SMSAdapter contactsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);
        ButterKnife.bind(this);
        initRecycler();

    }

    private void queryinfo() {
        new HttpUtil.Builder("message/getMsgContentByMobile")
                .Params("mobile", et_phone.getText().toString())
                .Tag(this)//需要取消请求的tag
                .Success(new Success() {
                    @Override
                    public void Success(String model) {
                        SMSBean contactUsBean = new Gson().fromJson(model, SMSBean.class);
                        if (contactUsBean.getFlag() == 1) {
                            contactsAdapter.setNewData(contactUsBean.getResult());
                        }

                    }


                }).ErrorEventFunc(new ErrorFunListener() {
            @Override
            public void ErrorFun(Object... values) {
                String value = (String) values[1];
            }
        })
                .get();
    }

    private void initRecycler() {
        rv_list.setLayoutManager(new LinearLayoutManager(this));
        contactsAdapter = new SMSAdapter(R.layout.sms_item, contactBeanArrayList);
        //todo 添加数据
        rv_list.setAdapter(contactsAdapter);
        contactsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                Intent intent = new Intent(SmsActivity.this, ShowTextActivity.class);
                intent.putExtra("data", contactBeanArrayList.get(position).getMessage_content());
                startActivity(intent);
            }
        });

    }

    @OnClick(R.id.btn_query)
    public void onClick() {
        queryinfo();

    }
}
